#include <vector>
#include <algorithm>
#include <future>
#include <iostream>

using namespace std;

const unsigned int NB_ELEMENTS_MAX = 100;

template <typename Iterator>
static long sum(Iterator begin, Iterator end) {
	typename Iterator::difference_type nb = end -begin;
	if (nb < NB_ELEMENTS_MAX) {
		return accumulate(begin, end, 0);
	}
	Iterator medium = begin + (nb / 2);
	future<long> s1 = async(sum<Iterator>, begin, medium);
	long s2 = sum(medium, end);
	return s2 + s1.get();
}

int main(int argc, char** argv) {
	if (argc > 1) {
		vector<long> v;
		long nb = atoi(argv[1]);
		for (long i = 1; i < nb; i++) {
			v.push_back(i);
		}
		cout << "Resultat = " << sum(v.begin(), v.end()) << endl;
	} else {
		cerr << "Arguments invalides" << endl;
	}
	return 0;
}

